package controllers;

import java.io.File;
import java.io.IOException;
import java.util.List;

import play.data.validation.Required;
import play.modules.spring.Spring;
import play.mvc.Controller;
import services.FileImportation;
import services.exception.InvalidDocumentException;



public class ImportMngt extends Controller {

	
    public static void home() {
        render();
    }
    
    public static void importSheet(@Required final List<File> sheetfile) throws InvalidDocumentException {
        
    	//TODO validation !
    	
    	FileImportation importService = Spring.getBeanOfType(FileImportation.class);
    	Long lastId = null;
    	try {
    		for (File file : sheetfile) {
    			lastId = importService.importSheet(Security.getConnectedUser(), file);
    		}
		} catch (IOException e) {
			throw new InvalidDocumentException(InvalidDocumentException.MSG_INVALID_DOCUMENT, e);
		} 
    	
    	
    	// aller sur la page de la (dernière) fiche de lecture créée !
		ReadingSheetMngt.displayOne(lastId);
    }

}
